package com.tqz.juc.atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>原子操作int类型
 *
 * @author tianqingzhao
 * @since 2022/2/5 20:25
 */
public class AtomicIntegerTest {
    
    /**
     * 多线程存在并发安全问题
     */
    static int intValue = 0;
    
    /**
     * cas保证原子操作
     */
    static AtomicInteger atomicValue = new AtomicInteger(0);
    
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    intValue++;
                    atomicValue.getAndIncrement();
                }
                
                countDownLatch.countDown();
            }).start();
        }
        
        countDownLatch.await();
        System.out.println(atomicValue.get());
        System.out.println(intValue);
        
       /* int[] value = new int[] {1, 2};
        AtomicIntegerArray integerArray = new AtomicIntegerArray(value);
        integerArray.addAndGet();*/
    }
    
}
